package com.pi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pi.constants.SystemConstants;
import com.pi.domain.ResponseResult;
import com.pi.domain.entity.Article;
import com.pi.domain.entity.Category;
import com.pi.domain.vo.ArticleDetailVo;
import com.pi.domain.vo.ArticleListVo;
import com.pi.domain.vo.HotArticleVo;
import com.pi.domain.vo.PageVo;
import com.pi.mapper.ArticleMapper;
import com.pi.service.ArticleService;
import com.pi.service.CategoryService;
import com.pi.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.pi.constants.SystemConstants.ARTICLE_STATUS_NORMAL;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    @Lazy
    private CategoryService categoryService;
    @Autowired
    private ArticleService articleService;

    @Override
    public ResponseResult hotArticleList() {
        // 构造查询条件
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        // 查询已发布文章
        queryWrapper.eq(Article::getStatus, ARTICLE_STATUS_NORMAL);
        // 按照浏览量进行降序排序
        queryWrapper.orderByDesc(Article::getViewCount);

        // 构造分页查询条件
        Page<Article> page = new Page<>(1,10);
        page = page(page, queryWrapper);
        List<Article> list = page.getRecords();

        // 封装成vo
//        List<HotArticleVo> hotArticleVos = new ArrayList<>();
//        for (Article article : list) {
//            HotArticleVo hotArticleVo = new HotArticleVo();
//            // 属性拷贝
//            BeanUtils.copyProperties(article, hotArticleVo);
//            hotArticleVos.add(hotArticleVo);
//        }
        List<HotArticleVo> hotArticleVos = BeanCopyUtils.copyBeanList(list, HotArticleVo.class);

        return ResponseResult.okResult(hotArticleVos);
    }

    @Override
    public ResponseResult articleList(Long categoryId, Integer pageNum, Integer pageSize) {

        // 查询正式发布的文章（status = 0,分页查询）
        // 定制的文章要显示在最前面（istop = 1且降序）

        // 构造查询条件
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(categoryId)&&categoryId>0 ,Article::getCategoryId,categoryId);
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        queryWrapper.orderByDesc(Article::getIsTop);


        // 构造分页查询条件
        Page<Article> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        // 通过categoryId查询categoryName
        List<Article> articles = page.getRecords();
//        articles.stream()
//                .map(article -> {
//                    if (categoryService != null && article.getCategoryId() != null) {
//                        Category category = categoryService.getById(article.getCategoryId());
//                        article.setCategoryName(
//                                (category != null && category.getName() != null) ?
//                                        category.getName() : "未知分类"
//                        );
//                    } else {
//                        article.setCategoryName("未知分类");
//                    }
//                    return article;
//                })
//                .collect(Collectors.toList());

        for (Article article : articles) {
            Category category = categoryService.getById(article.getCategoryId());
            article.setCategoryName(category.getName());
        }

        // 封装成VO
        // 先封装成rows，再封装成PageVo
        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(page.getRecords(), ArticleListVo.class);
        PageVo pageVo = new PageVo(articleListVos, page.getTotal());
        return ResponseResult.okResult(pageVo);


    }

    @Override
    public ResponseResult getArticleDetail(Long id) {
        // 查询文章列表
        Article article = getById(id);

        // 返回vo对象
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);

        // 通过categoryId查询categoryName

        Category category = categoryService.getById(article.getCategoryId());
        if (category != null) {
            articleDetailVo.setCategoryName(category.getName());
        }

        return ResponseResult.okResult(articleDetailVo);
    }
}
